package com.liucxu.zimuzu.View.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.liucxu.zimuzu.Model.Bean.ArticalEntity;
import com.liucxu.zimuzu.R;
import com.liucxu.zimuzu.Utils.DisplayUtil;
import com.liucxu.zimuzu.Utils.GlideUtils;
import com.liucxu.zimuzu.Utils.ViewHolderUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 资讯列表的adapter
 * Created by liukanshan on 2016/12/30.
 */

public class ArticalAdapter extends BaseAdapter {

    private List<ArticalEntity> datas = new ArrayList<>();
    private Context mContext;

    public ArticalAdapter(Context context, ArrayList<ArticalEntity> entities) {
        if (entities != null) {
            datas.clear();
            datas.addAll(entities);
        }
        mContext = context;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_hot, null);
        }
        initViews(position, convertView);
        return convertView;
    }

    private void initViews(int position, View view) {
        ArticalEntity entity = datas.get(position);
        ImageView imv = ViewHolderUtils.get(view, R.id.adapter_hot_imager);
        GlideUtils.getInstance().LoadSimpleContextRoundBitmap(mContext, entity.getPoster(), imv, DisplayUtil.px2dip(mContext, 14));
        TextView title = ViewHolderUtils.get(view, R.id.adapter_hot_title);
        title.setText(entity.getTitle());
        TextView adapter_en_name = ViewHolderUtils.get(view, R.id.adapter_en_name);
        Date artical_date = new Date(Long.parseLong(entity.getDateline() + "000"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(artical_date);
        adapter_en_name.setText("发布时间:" + time);
        TextView resource_desc = ViewHolderUtils.get(view, R.id.resource_desc);
        resource_desc.setText("说明:" + entity.getIntro());
        TextView resource_type = ViewHolderUtils.get(view, R.id.resource_type);
        resource_type.setText("类型:" + getArticalType(entity.getType()));
        TextView resource_score = ViewHolderUtils.get(view, R.id.resource_score);
        resource_score.setText("浏览:" + entity.getViews());
    }

    public void setDatas(ArrayList<ArticalEntity> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }

    private String getArticalType(String key) {
        String artical_type = "新闻";
        switch (key) {
            case "news":
                artical_type = "新闻";
                break;
            case "report":
                artical_type = "收视快报";
                break;
            case "m_review":
                artical_type = "影评";
                break;
            case "t_review":
                artical_type = "剧评";
                break;
            case "new_review":
                artical_type = "新剧评";
                break;
            case "recom":
                artical_type = "片单推荐";
                break;
            default:
                artical_type = "新闻";
                break;
        }
        return artical_type;
    }
}
